﻿using ProjBackEnd.Areas.BizPurview.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.BizPurview.Controllers
{
    public class ProjPurviewOptController : ApiController
    {
        /// <summary>
        /// 获取指定项目的成员列表
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|项目id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizpurview/projpurviewopt")]
        public RespProjPurview GetProjPurview(string key)
        {
            RespProjPurview resp = new RespProjPurview();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iProjid;
            if (!int.TryParse(strsKeyOriginal[3],out iProjid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iProjid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "项目编号不能为 0";
                return resp;
            }
            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = bizPurviewLogic.GetProjPurviewMember(iProjid);
            return resp;
        }

        /// <summary>
        /// 更新项目权限成员（仅增删）
        /// </summary>
        /// <param name="_reqInfo">项目成员更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizpurview/projpurviewopt")]
        public RespPurNullResultInfo UpdateProjPurview(ReqProjPurview _reqInfo)
        {
            RespPurNullResultInfo resp = new RespPurNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = bizPurviewLogic.UpdateProjPurview(_reqInfo.memberid, _reqInfo.purviewinfo);
            return resp;
        }


    }
}
